<template>
  <div class="row">
      <div class="col-xs-12 col-md-8 offset-md-2">
        <form class="card comment-form" @submit.prevent="addComment">
          <div class="card-block">
            <textarea class="form-control" placeholder="Write a comment..." rows="3" v-model.trim="comment"></textarea>
          </div>
          <div class="card-footer">
            <img :src="user.image" class="comment-author-img" />
            <button class="btn btn-sm btn-primary">
             Post Comment
            </button>
          </div>
        </form>

        <CommentItem ref="commentItem" @deleteComment="deleteComment"/>
      </div>
    </div>
</template>

<script>
import CommentItem from './CommentItem'
import {mapState} from 'vuex'
import {reqAddComment, reqDeleteComment} from '../../../api/article'
export default {
  name:'',
  data () {
    return {
       comment: ''
    }
  },
  computed: {
    ...mapState({
      user: state => state.user
    })
  },
  methods: {
    // 添加评论
    async addComment(){
      await reqAddComment(this.$route.params.aid, this.comment)
      this.comment = ''
      this.$refs.commentItem.getComment()
    },

    // 删除评论
    async deleteComment(id){
      let flag = confirm('您确定要删除这条评论吗？')
      if(flag){
        await reqDeleteComment(this.$route.params.aid, id)
        this.$refs.commentItem.getComment()
      }
    }
  },
  components:{
    CommentItem
  }
}

</script>
<style lang='less' scoped>
</style>